package com.darren.euc.util;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.darren.euc.entity.SystemParameter;
import com.darren.euc.service.SystemService;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

@Component 
public class CacheUtil {
	
	private CacheManager manager;
	
	private static final String path = "/ehcache.xml";
	
	private URL url;
	
	private static CacheUtil cacheUtil;
	
	private static SystemService systemService;
	
	private static Map<String,String> map = new HashMap<String,String>();
	
	public CacheUtil(){
		url = this.getClass().getResource(path);
		manager =CacheManager.create(url);
	}
	
	@Autowired  
    public void setSystemService(SystemService systemService) {  
		CacheUtil.systemService = systemService;  
    }  
	
	public CacheUtil(String path){
		url = this.getClass().getResource(path);
		manager =CacheManager.create(url);
	}
	
	public static CacheUtil getInstance(){
		if(cacheUtil == null){
			cacheUtil = new CacheUtil();
//			cacheUtil = new CacheUtil(path);
		}
		return cacheUtil;
	}
	

	public  Object get(String cacheName,String key){
		Cache cache = manager.getCache(cacheName);
		Element element = cache.get(key);
		return element == null ? null:element.getObjectValue();
	}

	public  void put(String cacheName,Object key, Object value){
		Cache cache = manager.getCache(cacheName);
		Element element = new Element(key,value);
		cache.put(element);
	}

	public  void remove(String cacheName,Object key){
		Cache cache = manager.getCache(cacheName);
		cache.remove(key);
		map = new HashMap<String,String>();
	}
	

	public  void clear(String cacheName){
		Cache cache = manager.getCache(cacheName);
		cache.removeAll();
	}
	

	@SuppressWarnings("unchecked")
	public static List<SystemParameter> getSystemParameterList(String cat){
		CacheUtil cacheUtil = CacheUtil.getInstance();
		List<SystemParameter> result = new ArrayList<SystemParameter>();
		List<SystemParameter> systemParameterList = (List<SystemParameter>)cacheUtil.get(Constant.CACHE_NAME_MEMBER, Constant.CACHE_SYSTEM_PARAMS);
		if(systemParameterList == null){
			CacheUtil.systemService.initalSystemParameter();
			systemParameterList = (List<SystemParameter>) cacheUtil.get(Constant.CACHE_NAME_MEMBER, Constant.CACHE_SYSTEM_PARAMS);
		}
		for(SystemParameter systemParameter:systemParameterList){
			String _cat = systemParameter.getLookupCat();
			if(cat.equalsIgnoreCase(_cat)){
				result.add(systemParameter);
			}
		}
		return result;
	}
	

	@SuppressWarnings("unchecked")
	public static SystemParameter getSystemParameter(String cat,String type){
		CacheUtil cacheUtil = CacheUtil.getInstance();
		List<SystemParameter> paramsList = (List<SystemParameter>)cacheUtil.get(Constant.CACHE_NAME_MEMBER, Constant.CACHE_SYSTEM_PARAMS);
		if(paramsList == null){
			CacheUtil.systemService.initalSystemParameter();
			paramsList = (List<SystemParameter>)cacheUtil.get(Constant.CACHE_NAME_MEMBER, Constant.CACHE_SYSTEM_PARAMS);
		}
		for(SystemParameter sysParam:paramsList){
			String _cat = sysParam.getLookupCat();
			String _type = sysParam.getLookupType();
			if(cat.equalsIgnoreCase(_cat) && type.equalsIgnoreCase(_type)){
				return sysParam;
			}
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public static String getSysParamVal(String cat,String type){
		CacheUtil cacheUtil = CacheUtil.getInstance();
		String result = "";
		List<SystemParameter> paramsList = (List<SystemParameter>)cacheUtil.get(
				Constant.CACHE_NAME_MEMBER, Constant.CACHE_SYSTEM_PARAMS);
		if(paramsList == null){
			CacheUtil.systemService.initalSystemParameter();
			map = new HashMap<String,String>();
			paramsList = (List<SystemParameter>) cacheUtil.get(Constant.CACHE_NAME_MEMBER, Constant.CACHE_SYSTEM_PARAMS);
		}
		if(map.size()<1){
			for(SystemParameter systemParameter:paramsList){
				String _type = systemParameter.getLookupType();
				map.put(_type, systemParameter.getLookupValue());
			}
		}
		result = map.get(type);
		return result;
	}
	

}
